package com.wjf.birdfish.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.wjf.birdfish.commons.Constants;
import com.wjf.birdfish.util.StringHelper;

/** 
 * Created on 2007-4-11
 * Title: <p/>
 * Description: <p/>
 * Copyright: Copyright TYDIC (c) 2007<p/>
 * Company: tydic.com<p/>
 * @author <a href="mailto:tongys@tydic.com">	童永胜</a>
 * @version 1.0
 */
public abstract class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, Preparable  {

	private static final long serialVersionUID = 1L;
    protected transient final Log log = LogFactory.getLog(getClass());

    private List<String> fieldList = new ArrayList<String>();
    
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    
    public static final String LIST = "list";
    public static final String EDIT = "edit";
    public static final String RECORDLIST = "recordlist";
    public static final String BACK = "back";
    public static final String VIEW = "view";  
    
    protected String[] flag_id;
    
    
    /** 当前分页页码 */ 
    protected int pageNo = 1;
    
    /** 默认的分页页数(12) */
    protected int pageSize = Constants.DEFAULT_PAGE_SIZE;

    @SuppressWarnings("unchecked")
	protected void saveMessage(String msg) {
        List<String> messages = (List<String>) getRequest().getAttribute("messages");
        if (messages == null) {
            messages = new ArrayList<String>();
        }
        messages.add(msg);
        getRequest().setAttribute("messages", messages);
    }
    
//    protected void saveMessage(Message msgEx) {
//    	getSession().setAttribute(Message.MESSAGE_KEY, msgEx);
//    }
    
    /**
     * 得到保存在session中的配置HashMap
     */
    protected Map getConfiguration() {
        Map config = (HashMap) getSession().getServletContext().getAttribute(Constants.CONFIG);
        // so unit tests don't puke when nothing's been set
        if (config == null) {
            return new HashMap();
        }
        return config;
    }
    
    protected void saveError(String msg) {
    	addActionError(msg);
    }
    
    protected void saveError(String msg, String fieldName) {
    	saveError(msg);
    	fieldList.add(fieldName);
    }
    
    protected void saveError(String msg, Object ...fieldNames) {
    	saveError(msg);
    	if (fieldNames != null) {
    		for (Object fieldName : fieldNames) {
    			fieldList.add(fieldName.toString());
    		}
    	}
    }
    
    /**
     * 校验字符串字段的最大长度，支持中文字符，每个汉字占2个字节长度
     * @param fieldName 字段的中文名称
     * @param field	要校验的字段
     * @param maxLen 最大长度
     */
    protected void saveErrorForLength(String fieldName, String field, int maxLen) {
    	if (StringUtils.isNotBlank(field)) {
    		if (StringHelper.strlen(field) > maxLen) {
    			saveError(fieldName + "的长度不能大于" + maxLen +"字符"+ ",当前输入长度为" + 
    					StringHelper.strlen(field)+"字符");
    			//saveError(fieldName + "过长！" );
    		//	System.err.println("校验结束！！！");
    		}
    	}
    }
    
    protected HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();  
    }
    
    protected HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }
    
    protected HttpSession getSession() {
    	return getRequest().getSession();
    }
    
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
		this.session = arg0.getSession();
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

	public String[] getFlag_id() {
		return flag_id;
	}

	public void setFlag_id(String[] flag_id) {
		this.flag_id = flag_id;
	}

	public List<String> getFieldList() {
		return fieldList;
	}

	public void setFieldList(List<String> fieldList) {
		this.fieldList = fieldList;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}


	
	
	 public void renderText(String text) {
	        try {
	            response.setContentType("text/plain;charset=GBK");
	            response.getWriter().write(text);
	        } catch (IOException e) {
	            log.error(e);
	        }
	    }

	    /**
	     * 直接输出纯HTML
	     */
	    public void renderHtml(String text) {
	        try {
	            response.setContentType("text/html;charset=GBK");
	            response.getWriter().write(text);
	        } catch (IOException e) {
	            log.error(e);
	        }
	    }

	    /**
	     * 直接输出纯XML
	     */
	    public void renderXML(String text) {
	        try {
	            response.setContentType("text/xml;charset=GBK");
	            response.getWriter().write(text);
	        } catch (IOException e) {
	            log.error(e);
	        }
	    }




}